package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.mapper.ItemMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.vo.ItemVO;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ItemServiceImpl implements ItemService{
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemDescMapper itemDescMapper;
    @Override
    public PageResult getItemList(PageResult pageResult){
        //1.总数
        long total=pageResult.getTotal();
        //2.分页结果
        int size=pageResult.getPageSize();
        int start=(pageResult.getPageNum()-1)*size;
        String query=pageResult.getQuery();
        List<Item> list=itemMapper.getItemList(start,size,query);
        return pageResult.setTotal(total).setRows(list);
    }
    @Override
    @Transactional
    public void updateItemStatusMP(Item item){
        int id=item.getId();
        boolean status=item.getStatus();
        UpdateWrapper<Item> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        item.setStatus(status);
        itemMapper.update(item,updateWrapper);
    }
    @Override
    @Transactional
    public void deleteItemById(Integer id){
        itemMapper.deleteById(id);
        itemDescMapper.deleteById(id);
    }
    /**
     * 语法:selectPage语法说明
     *      IPage   工具API
     *      1.page: MP内部指定的分页对象
     *      2.queryWrapper 条件构造器
     *      Sql: where titile like "%xxx%"
     * @param pageResult
     * @return
     */
    @Override
    public PageResult getItemListMP(PageResult pageResult){
        //检测 title字符串是否有值
        boolean flag= StringUtils.hasLength(pageResult.getQuery());
        QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(flag,"title",pageResult.getQuery());

        //编辑 MP的分页对象（页数/条数/总数/记录）    传递=页数/条数
        IPage<Item> page=new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        //返回值为IPage<Item>
        page=itemMapper.selectPage(page,queryWrapper);
        long total=page.getTotal();
        List<Item> rows=page.getRecords();

        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    @Transactional
    public void saveItemMP(ItemVO itemVO){
        //由于item.id=itemDesc.id 所以ID不能主键自增
        Item item=itemVO.getItem();
        //要求item入库后，动态返回ID！！！！
        //MP原则：入库后动态回显数据！！
        itemMapper.insert(item);
        //实现item_desc入库
        ItemDesc itemDesc=itemVO.getItemDesc();
        itemDesc.setId(item.getId());
        itemDescMapper.insert(itemDesc);
    }
    @Override
    @Transactional
    public void updateItem(Item item){
        UpdateWrapper<Item> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",item.getId());
        itemMapper.update(item,updateWrapper);
    }
}

